/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tpa.TablaPeriodica;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

/**
 *
 * @author leandro
 */
@Entity
public class SubClase implements Serializable
{

    @ManyToMany(mappedBy = "propiedades", cascade=CascadeType.REMOVE)
    private List<Elemento> elementos;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String valor;
    @ManyToOne
    private Clasificacion parentClasificacion;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SubClase))
        {
            return false;
        }
        SubClase other = (SubClase) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        String toString = "";

        if(parentClasificacion != null)
            toString = parentClasificacion.getNombre() + " : ";
        
        toString += valor + "(" + id + ")";
        
        return toString;
    }

    /**
     * @return the valor
     */
    public String getValor()
    {
        return valor;
    }

    /**
     * @param valor the valor to set
     */
    public void setValor(String valor)
    {
        this.valor = valor;
    }

    public void add(Elemento value)
    {
        if (value != null && !this.getElementos().contains(value))
        {
            this.getElementos().add(value);
            value.addProperty(this);
        }
    }

    public void remove(Elemento value)
    {
        if (this.getElementos().contains(value))
        {
            this.getElementos().remove(value);
            value.removeProperty(this);
        }
    }

    public boolean contains(Long elementoNumeroAtomico)
    {
        for(Elemento currentElemento : this.getElementos())
            if(currentElemento.getNumeroAtomico() == elementoNumeroAtomico)
                return true;

        return false;
    }

    public String getNombre()
    {
        return this.getParentClasificacion().getNombre();
    }

    /**
     * @return the parentClasificacion
     */
    public Clasificacion getParentClasificacion()
    {
        return parentClasificacion;
    }

    /**
     * @param parentClasificacion the parentClasificacion to set
     */
    public void setParentClasificacion(Clasificacion parentClasificacion)
    {
        this.parentClasificacion = parentClasificacion;
    }

    /**
     * @return the elementos
     */
    public List<Elemento> getElementos()
    {
        if (elementos == null)
            elementos = new ArrayList<Elemento>();

        return elementos;
    }
}
